﻿Option Strict On
Option Infer On
Option Explicit On

Imports System
Imports System.Xml.Linq
Imports System.Collections.Generic
Imports GenDotNet.StoredProcModel
Imports GenDotNet.Task.Common
Imports <xmlns:orm="http://kadgen.com/KADORM.xsd">

Public Class ColumnData
   Inherits BaseData
   Implements IColumnData

   Private mTableName As String
   Private mColumnName As String

   Public Shared Function Load(ByVal rootdata As RootData, ByVal elements As IEnumerable(Of XElement)) _
        As IEnumerable(Of IColumnData)
      Dim list = New List(Of IColumnData)
      For Each e In elements
         list.Add(New ColumnData(rootdata, e))
      Next
      Return list
   End Function

   Private Sub New( _
            ByVal rootdata As RootData, _
            ByVal element As XElement)
      MyBase.New(rootdata, element)
   End Sub

   Friend Sub New( _
            ByVal rootData As RootData, _
            ByVal tableName As String, _
            ByVal columnName As String)
      MyBase.New(rootData, Nothing, True)
      Me.mTableName = tableName
      Me.mColumnName = columnName
   End Sub


   Public ReadOnly Property AllowNulls() _
            As Boolean _
            Implements IColumnData.AllowNulls
      Get
         If Me.Element Is Nothing Then
            Return True
         End If
         Return Me.Element.@AllowNulls.IsTrue
      End Get
   End Property

   Public ReadOnly Property Caption() _
            As String _
            Implements IColumnData.Caption
      Get
         If Me.Element Is Nothing Then
            ' Future call MakeCaption function
            Return Me.Name
         End If
         Return Me.Element.@Caption
      End Get
   End Property

   'Public ReadOnly Property ColumnName() _
   '         As String _
   '         Implements IColumnData.ColumnName
   '   Get
   '      If Me.Element Is Nothing Then
   '         Return Me.mColumnName
   '      End If
   '      Return Me.Element.@Column
   '   End Get
   'End Property

   Public ReadOnly Property IsAutoIncrement() _
            As Boolean _
            Implements IColumnData.IsAutoIncrement
      Get
         If Me.Element Is Nothing Then
            Return False
         End If
         Return Me.Element.@IsAutoIncrement.IsTrue
      End Get
   End Property

   Public ReadOnly Property IsPrimaryKey() _
            As Boolean _
            Implements IColumnData.IsPrimaryKey
      Get
         If Me.Element Is Nothing Then
            Return False
         End If
         Return Me.Element.@IsPrimaryKey.IsTrue
      End Get
   End Property

   Public ReadOnly Property MaxLength() _
            As Int32 _
            Implements IColumnData.MaxLength
      Get
         If Me.Element Is Nothing Then
            Return 0
         End If
         Return CInt(Me.Element.@MaxLength)
      End Get
   End Property

   Public ReadOnly Property Name() _
            As String _
            Implements IColumnData.Name
      Get
         If Me.Element Is Nothing Then
            Return Me.mColumnName
         End If
         Return Me.Element.@Column
      End Get
   End Property

   Public ReadOnly Property Ordinal() _
            As Int32 _
            Implements IColumnData.Ordinal
      Get
         If Me.Element Is Nothing Then
            Return 0
         End If
         Return CInt(Me.Element.@Ordinal)
      End Get
   End Property

   Public ReadOnly Property Precision() _
            As Int32 _
            Implements IColumnData.Precision
      Get
         If Me.Element Is Nothing Then
            Return 0
         End If
         Return CInt(Me.Element.@Precision)
      End Get
   End Property

   Public ReadOnly Property Scale() _
            As Int32 _
            Implements IColumnData.Scale
      Get
         If Me.Element Is Nothing Then
            Return 0
         End If
         Return CInt(Me.Element.@Scale)
      End Get
   End Property

   Public ReadOnly Property SQLType() _
            As String _
            Implements IColumnData.SQLType
      Get
         If Me.Element Is Nothing Then
            Return String.Empty
         End If
         Return Me.Element.@SQLType
      End Get
   End Property

   Public ReadOnly Property Table() _
            As ITableData _
            Implements StoredProcModel.IColumnData.Table
      Get
         Dim tableName As String
         If Me.Element Is Nothing Then
            tableName = mTableName
         Else
            tableName = Me.Element.@TableName
         End If
         Return RootData.GetTable(String.Empty, tableName)
      End Get
   End Property

   Public ReadOnly Property ColumnAlias() _
            As String _
            Implements IColumnData.ColumnAlias
      Get
         Dim ret As String
         If Me.Element Is Nothing Then
            ret = Me.mColumnName
         Else
            ret = Me.Element.@Alias
            If String.IsNullOrEmpty(ret) Then
               Return Me.Name
            End If
         End If
         Return ret
      End Get
   End Property


   Public ReadOnly Property IsReadOnly() _
            As Boolean _
            Implements IColumnData.IsReadOnly
      Get
         ' TODO: This should reflect JoinBase but not JoinIn columns which are not currently updated
         Return (Me.Element.@IsAutoIncrement.IsTrue Or Me.Element.@IsComputed.IsTrue)
      End Get
   End Property
End Class
